<?php

/**
 * @classname 排队
 * @classaccess public
 * @decription 排队接口
 * @author 张旭刚
 * @updateTime 2020年05月12日
 */

namespace employee\controllers;

use Yii;
use employee\models\Queues;

class QueuesController extends PublicController {

    /**
     * @see 排队标识核销
     * @describe 排队码核销接口
     * @access public
     * @name verification
     * @method POST
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2020年07月03日,创建
     * 
     * @header guid|string|required|客户端唯一编号
     * @header userToken|string|required|登录令牌
     * 
     * @param queueKey|string|required|排队编号
     * @param force|string|unrequired|是否强行核销|是/否：true/false
     * 
     * @return result|string|成功/失败（true/false）
     */
    public function actionVerification(){
        $queueKey = Yii::$app->request->post('queueKey');
        if(empty($queueKey)){
            return $this->renderJson(['code' => 0, 'msg' => '排队标识不能为空', 'result' => false, 'errorCode' => 204001]);
        }
        $force = Yii::$app->request->post('force');
        $m = Queues::findOne(['queueKey' => $queueKey]);
        if(empty($m)){
            return $this->renderJson(['code' => 0, 'msg' => '未找到排队信息', 'result' => false, 'errorCode' => 204001]);
        }
        if($m->status == 2){
            return $this->renderJson(['code' => 0, 'msg' => '排队信息已经已入场，不能重复入场', 'result' => false, 'errorCode' => 204002]);
        }
        // $this->renderJson([$force]);
        if($force == 'false'){
            if($m->call == 1){
                return $this->renderJson(['code' => 0, 'msg' => '排队的信息还未叫号', 'result' => false, 'errorCode' => 204005]);
            }
            if($m->status == 3 && $m->call == 3){
                return $this->renderJson(['code' => 0, 'msg' => '排队信息已过期', 'result' => false, 'errorCode' => 204003]);
            }
        }
        $m->status = 2;
        if($m->save(false)){
            return $this->renderJson(['code' => 1, 'msg' => '入场成功', 'result' => true, 'errorCode' => 0]);
        }
        return $this->renderJson(['code' => 0, 'msg' => '入场失败', 'result' => false, 'errorCode' => 204004]);
    }

    /**
     * @see 排队码核销
     * @describe 排队码核销接口
     * @access public
     * @name verification-no
     * @method POST
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2020年07月03日,创建
     * 
     * @header guid|string|required|客户端唯一编号
     * @header userToken|string|required|登录令牌
     * 
     * @param queueNo|string|required|排队编号
     * @param force|string|unrequired|是否强行核销|是/否：true/false
     * 
     * @return result|string|成功/失败（true/false）
     */
    public function actionVerificationNo(){
        $queueNo = Yii::$app->request->post('queueNo');
        if(empty($queueNo)){
            return $this->renderJson(['code' => 0, 'msg' => '排队标识不能为空', 'result' => false, 'errorCode' => 204001]);
        }
        $force = Yii::$app->request->post('force');
        $m = Queues::findOne(['queueNo' => $queueNo, 'inData' => date('Y-m-d')]);
        if(empty($m)){
            return $this->renderJson(['code' => 0, 'msg' => '未找到排队信息', 'result' => false, 'errorCode' => 204001]);
        }
        if($m->status == 2){
            return $this->renderJson(['code' => 0, 'msg' => '排队信息已经已入场，不能重复入场', 'result' => false, 'errorCode' => 204002]);
        }
        
        if($force == 'false'){
            if($m->call == 1){
                return $this->renderJson(['code' => 0, 'msg' => '排队的信息还未叫号', 'result' => false, 'errorCode' => 204005]);
            }
            if($m->status == 3 && $m->call == 3){
                return $this->renderJson(['code' => 0, 'msg' => '排队信息已过期', 'result' => false, 'errorCode' => 204003]);
            }
        }
        $m->status = 2;
        if($m->save(false)){
            return $this->renderJson(['code' => 1, 'msg' => '入场成功', 'result' => true, 'errorCode' => 0]);
        }
        return $this->renderJson(['code' => 0, 'msg' => '入场失败', 'result' => false, 'errorCode' => 204004]);
    }
}
